<template>
	<section>
		<!--工具条-->
		<el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
			<el-form :inline="true" :model="filters">
				<el-form-item>
					<el-input v-model="filters.keyWords" placeholder="姓名"></el-input>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" v-on:click="getShops">查询</el-button>
				</el-form-item>
			</el-form>
		</el-col>

		<!--列表-->
		<el-table :data="shops" highlight-current-row v-loading="listLoading" style="width: 100%;">
			<el-table-column type="selection" width="55">
			</el-table-column>
			<el-table-column type="index" width="60">
			</el-table-column>
			<el-table-column prop="name" label="店铺名称" width="120" sortable>
			</el-table-column>
			<el-table-column prop="tel" label="店铺电话" width="120" sortable>
			</el-table-column>
			<el-table-column prop="registerTime" label="注册时间" width="120" sortable>
			</el-table-column>
			<el-table-column prop="address" label="店铺地址" width="120" sortable>
			</el-table-column>
			<el-table-column prop="admin.username" label="店长姓名" width="120" sortable>
			</el-table-column>
			<el-table-column prop="state" label="状态" width="100" sortable>
				<template scope="scope">
					<span style="color: green" v-if="scope.row.state==0">待审批</span>
					<span style="color: red" v-else-if="scope.row.state==-1">禁用</span>
					<span style="color: blue" v-else-if="scope.row.state==1">审批通过</span>
					<span style="color: grey" v-else-if="scope.row.state==2">驳回</span>
					<span v-else>未知</span>
				</template>
			</el-table-column>
			<el-table-column label="操作" width="150">
				<template scope="scope">
					<el-button size="small" @click="passEdit(scope.$index, scope.row)">审批</el-button>
					<el-button type="button" size="small" @click="changeState(scope.$index, scope.row)">
						<span style="color: red" v-if="scope.row.state!=-1">禁用</span>
						<span style="color: green" v-else-if="scope.row.state==-1">启用</span>
					</el-button>
				</template>
			</el-table-column>
		</el-table>

		<!--工具条-->
		<el-col :span="24" class="toolbar">
			<el-pagination layout="prev, pager, next" @current-change="handleCurrentChange" :page-size="pageSize" :total="total" style="float:right;">
			</el-pagination>
		</el-col>

		<!--审批界面模态框-->
		<el-dialog :title="title" :visible.sync="editFormVisible" :close-on-click-modal="false">
			<el-form :model="editForm" label-width="80px" :rules="editFormRules" ref="editForm">
				<el-form-item>
					<el-radio-group v-model="editForm.state">
						<el-radio-button label="1">审批通过</el-radio-button>
						<el-radio-button label="2">驳回</el-radio-button>
					</el-radio-group>
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click.native="editFormVisible = false">取消</el-button>
				<el-button type="primary" @click.native="editSubmit" :loading="editLoading">提交</el-button>
			</div>
		</el-dialog>


	</section>
</template>

<script>
	import util from '../../common/js/util'
	//import NProgress from 'nprogress'
	import { getDepartmentListPage, removeDepartment, batchRemoveDepartment, editDepartment, addDepartment } from '../../api/api';

	export default {
		data() {
			return {
				filters: {
                    keyWords: ''
				},
                shops: [],
                employees: [],
                pageSize:5,
				total: 0,
                localPage: 1,
				listLoading: false,
				sels: [],//列表选中列
                title: "",
				editFormVisible: false,//编辑界面是否显示
				editLoading: false,
				editFormRules: {
					name: [
						{ required: true, message: '请输入店铺名称', trigger: 'blur' }
					],
					tel: [
						{ required: true, message: '请输入电话', trigger: 'blur' }
					],
					address: [
						{ required: true, message: '请输入店铺地址', trigger: 'blur' }
					]
				},
				//编辑界面数据
				editForm: {
                    state: 1,
				},

				addFormVisible: false,//新增界面是否显示
				addLoading: false,
				addFormRules: {
					name: [
						{ required: true, message: '请输入姓名', trigger: 'blur' }
					]
				}

			}
		},
		methods: {
			//性别显示转换
			/*formatSex: function (row, column) {
				return row.sex == 1 ? '男' : row.sex == 0 ? '女' : '未知';
			},*/
			handleCurrentChange(val) {
				this.localPage = val;
				this.getShops();
			},
            // 获取员工列表
            getEmployees(){
                this.$http.get("/emp")
                    .then(result=>{
                        this.employees = result.data;
                    }).catch(result=>{
                    alert("系统繁忙，请稍后再试")
                })
            },

			//获取店铺列表
			getShops() {
				let para = {
                    localPage: this.localPage,
                    keyWords: this.filters.keyWords
				};
				this.listLoading = true; // 打开忙等框


				this.$http.post("/shop",para)
					.then(result=>{
					    this.shops = result.data.list;
					    this.total = result.data.totals;

                        this.listLoading = false; // 关闭忙等框
					}).catch(result=>{
					    alert("系统繁忙，请稍后再试")
				})
			},
			// 修改状态
            changeState: function (index, row) {
                this.$confirm('确认修改状态吗?', '提示', {
                    type: 'warning'
                }).then(() => {
                    if(row.state==1){
                        row.state=-1;
                    }else {
                        row.state=1;
                    }
                    this.$http.put("/shop/",row)
                        .then(result=>{
                            if(result.data.success){
                                this.$message({
                                    message: '操作成功',
                                    type: 'success'
                                });
                                this.getShops();
                            } else {
                                this.$message({
                                    message: result.data.msg,
                                    type: 'error'
                                });
                            }
                        }).catch(result=>{
                        this.$message({
                            message: "系统繁忙",
                            type: 'error'
                        });
                    })
                }).catch(() => {
                });
            },
			//显示审核界面
            passEdit: function (index, row) {
			    if(row.state ==0){
                    this.title = "审核";
                    this.editFormVisible = true;
                    this.editForm = Object.assign({}, row); // 回显数据
				} else {
			        alert("已审核，请勿重复操作")
				}

			},
			// 审批发起请求
			editSubmit: function () {
				this.$refs.editForm.validate((valid) => {

					if (valid) {
						this.$confirm('确认提交吗？', '提示', {}).then(() => {
							let para = Object.assign({}, this.editForm); // 克隆表单数据
							this.$http.put("/shop",para)
								.then(result=>{
                                    this.editLoading = false; // 关闭忙等框
								    if(result.data.success){

                                        this.$message({
                                            message: '提交成功',
                                            type: 'success'
                                        });
                                    } else {
                                        this.$message({
                                            message: result.data.msg,
                                            type: 'error'
                                        });
									}
                                    this.editFormVisible = false; // 关闭
                                    this.getShops();
							}).catch(result=>{
                                this.editLoading = false
                                this.editFormVisible = false
                                this.$message({
                                    message: '系统异常',
                                    type: 'error'
                                });
							})
						});
					}
				});
			}
		},
		mounted() {
			this.getShops();
			this.getEmployees();
		}
	}

</script>

<style scoped>

</style>